$(function() {
	/*===图片懒加载===*/
	start();
	$(window).on('scroll', function() {
		start();
	});
	/*===图片轮播===================================================*/
	var swiperCase = new Swiper('.caseSwi .swiper-container', {
		slidesPerView: 'auto',
		spaceBetween: 10,
		loop: true,
		effect: 'effect',
		centeredSlides: true,
	});
	var partnerSwi = new Swiper('.partnerSwi .swiper-container', {
		// pagination: '.swiper-pagination',
		// paginationClickable: true,
		slidesPerView: 3,
		slidesPerColumn: 3,
		spaceBetween: 15
	});
	/*===nav===*/
	$(".muneBtn").click(function(){
		$("#navfull").addClass("active");
	});
	$("#navclose").click(function(){
		$("#navfull").removeClass("active");
	});
	/*===案例===*/
	$("#kindsIcon").click(function(){
		$("#kindsList").stop().slideDown();
		$(this).stop().fadeOut();
	});
	$("#choose").click(function(){
		$("#kindsList").stop().slideUp();
		$("#kindsIcon").stop().fadeIn();
	});
	$("#fixshow").click(function(){
		$("#kindsList").stop().slideDown();
	});
	/*===表单===*/
	$("#formBtn").click(function(){
		$("#formTc").stop().fadeIn();
	});
	$("#closeForm").click(function(){
		$("#formTc").stop().fadeOut();
	});
	$("#closeSucTc").click(function(){
		$("#formSucTc").stop().fadeOut();
	});
	/*===表单提交===================================================*/
	$("#subForm").click(function(){
		var mobile = $("#mobile").val();
		if(!mobile){
			layer.msg("手机号不能为空!");
			return false;
		}
		var reg = /(1[3-9]\d{9}$)/;
		if(!reg.test(mobile)) {
			layer.msg("手机号格式不正确!");
			return false;
		}
		//请求接口成功
		$("#formTc").stop().fadeOut();
		$("#formSucTc").stop().fadeIn();
		
	});
	/*===tab切换===================================================*/
	$(".wxMo1 .tabBars .tabBar").eq(0).addClass("active");
	$(".wxMo1 .tabCons .tabCon").eq(0).addClass("active");
	tabChange(".wxMo1 .tabBars .tabBar",".wxMo1 .tabCons .tabCon","active");
	
	//计数
	var winWidth = document.body.clientWidth;
	var isCount = true;
	if (isCount && $('.indexNum').length > 0) {
		if (isShow($(".indexNum"))) {
			$(".timer").each(function(i) {
				var num = $(this).attr("data-to");
				$(this).animateNumbers(num, false, 1000, "linear");
				isCount = false;
			});
		}
	};
	//滑动
	var height = document.documentElement.clientHeight || document.body.clientHeight;
	var thisS = [];
	var titLen = $(".title").length;
	var scrTop = document.documentElement.scrollTop || document.body.scrollTop;
	for (var i = 0; i < titLen; i++) {
		thisS[i] = $(".title").eq(i).offset().top - scrTop;
		if ((height - thisS[i]) >= 130) {
			if (!$(".title").eq(i).hasClass("animated lineTran")) {
				$(".title").eq(i).addClass("animated lineTran").removeClass("animate");
				$(".title").eq(i).next(".animateBox").removeClass("animate-up").addClass("animated");
			}
		} else {
			if ((height < thisS[i])) {
				if ($(".title").eq(i).hasClass("animated lineTran")) {
					$(".title").eq(i).addClass("animate").removeClass("animated lineTran");
					$(".title").eq(i).next(".animateBox").removeClass("animated").addClass("animate-up");
				}
	
			}
		}
	};
	window.onscroll = function() {
		var j = document.documentElement.scrollTop || document.body.scrollTop;
		if (j > 0) {
			if (!$(".header").hasClass("active")) {
				$(".header").addClass("active")
			}
			$("#backTop").fadeIn();
		} else {
			if (j === 0) {
				$(".header").removeClass("active")
			}
			$("#backTop").fadeOut();
		}
		for (var i = 0; i < titLen; i++) {
			thisS[i] = $(".title").eq(i).offset().top - j;
			if ((height - thisS[i]) >= 130) {
				if (!$(".title").eq(i).hasClass("animated lineTran")) {
					$(".title").eq(i).addClass("animated lineTran").removeClass("animate");
					$(".title").eq(i).next(".animateBox").removeClass("animate-up").addClass("animated");
				}
			} else {
				if ((height < thisS[i])) {
					if ($(".title").eq(i).hasClass("animated lineTran")) {
						$(".title").eq(i).addClass("animate").removeClass("animated lineTran");
						$(".title").eq(i).next(".animateBox").removeClass("animated").addClass("animate-up");
					}
	
				}
			}
		}
		if (isCount && $('.indexNum').length > 0) {
			if (isShow($(".indexNum"))) {
				$('.timer').each(count);
				isCount = false;
			};
		};
		//案例
		if($(".caseKinds").length > 0){
			var hi = $(".indexBanner").height();
			console.log(hi);
			if (j > hi) {
				$(".caseKinds").addClass('fixed');		
				$('#kindsIcon').hide();
				$('.caseKinds .fixshow').stop().fadeIn();				
			}else{
				$(".caseKinds").removeClass('fixed');
				$('.caseKinds .fixshow').stop().fadeOut();
				if($('#kindsList').is(":hidden")){
					$('#kindsIcon').stop().fadeIn();		
				}
			}
		}
		
	};
	// 双向轮播
	$(".preview .swiper-slide").eq(0).addClass("active-nav");
	var viewSwiper = new Swiper('.view .swiper-container', {
		effect: 'effect',
		slidesPerView: 'auto',
		// autoplay: 6000,
		// autoplayDisableOnInteraction: false,
		// coverflow: {
		// 	rotate: 0, // 旋转的角度
		// 	stretch: 160, // 拉伸   图片间左右的间距和密集度
		// 	depth: 150, // 深度   切换图片间上下的间距和密集度
		// 	modifier: 3, // 修正值 该值越大前面的效果越明显
		// 	slideShadows: false // 页面阴影效果
		// },
		onSlideChangeStart: function() {
			updateNavPosition();
		}
	});
	$('.view .arrow-left,.preview .arrow-left').on('click', function(e) {
		e.preventDefault()
		if (viewSwiper.activeIndex == 0) {
			//			viewSwiper.slideTo(viewSwiper.slides.length - 1, 1000);
			return
		}
		viewSwiper.slidePrev()
	})
	$('.view .arrow-right,.preview .arrow-right').on('click', function(e) {
		e.preventDefault()
		if (viewSwiper.activeIndex == viewSwiper.slides.length - 1) {
			//			viewSwiper.slideTo(0, 1000);
			return
		}
		viewSwiper.slideNext()
	})
	
	var previewSwiper = new Swiper('.preview .swiper-container', {
		//visibilityFullFit: true,
		slidesPerView: 'auto',
		noSwiping: true
	})
	$(".appMo2 .preview .swiper-slide").click(function() {
		viewSwiper.slideTo($(this).index());
		previewSwiper.slideTo($(this).index());
	});
	$(".appMo2 .swi").click(function() {
		previewSwiper.stopAutoplay();
		viewSwiper.stopAutoplay();
	})
	
	function updateNavPosition() {
		$('.preview .active-nav').removeClass('active-nav')
		var activeNav = $('.preview .swiper-slide').eq(viewSwiper.activeIndex).addClass('active-nav')
		if (!activeNav.hasClass('swiper-slide-visible')) {
			if (activeNav.index() > previewSwiper.activeIndex) {
				var thumbsPerNav = Math.floor(previewSwiper.width / activeNav.width()) - 1
				previewSwiper.slideTo(activeNav.index() - thumbsPerNav)
			} else {
				previewSwiper.slideTo(activeNav.index())
			}
		}
	}
	
});
/*===图片懒加载===*/
function start() {
	$('img').not('[data-isLoaded]').each(function() {
		var $node = $(this)
		if (isShow($node)) {
			loadImg($node)
		}
	})
}
function isShow($node) {
	return $node.offset().top <= $(window).height() + $(window).scrollTop();
}
function loadImg($img) {
	$img.attr('src', $img.attr('data-src'));
	$img.attr('data-isLoaded', 1);
}
/*===tab切换===*/
function tabChange(tab, con, ac) {
	$(tab).click(function() {
		var i = $(this).index();
		$(this).addClass(ac).siblings().removeClass(ac);
		$(con).eq(i).addClass(ac).siblings().removeClass(ac);
	});
}
 (function($) {
 	$.fn.animateNumbers = function(stop, commas, duration, ease) {
 		return this.each(function() {
 			var $this = $(this);
 			var start = parseInt($this.text().replace(/,/g, ""));
 			commas = (commas === undefined) ? true : commas;
 			$({
 				value: start
 			}).animate({
 				value: stop
 			}, {
 				duration: duration == undefined ? 1000 : duration,
 				easing: ease == undefined ? "swing" : ease,
 				step: function() {
 					$this.text(Math.floor(this.value));
 					if (commas) {
 						$this.text($this.text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
 					}
 				},
 				complete: function() {
 					if (parseInt($this.text()) !== stop) {
 						$this.text(stop);
 						if (commas) {
 							$this.text($this.text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
 						}
 					}
 				}
 			});
 		});
 	};
 })(jQuery);